<?php

namespace Hrm\Models;

use Helpers\BaseModel;
use Helpers\Tool;
use Phalcon\Di;

class Payroll extends BaseModel
{

    /**
     *
     * @var integer
     */
    public $id;

    /**
     *
     * @var integer
     */
    public $userId;

    /**
     *
     * @var integer
     */
    public $year;

    /**
     *
     * @var integer
     */
    public $month;

    /**
     *
     * @var string
     */
    public $key;

    /**
     *
     * @var string
     */
    public $basicWage;

    /**
     *
     * @var string
     */
    public $phoneWage;

    /**
     *
     * @var string
     */
    public $totalSub;

    /**
     *
     * @var string
     */
    public $overtimeSub;

    /**
     *
     * @var string
     */
    public $kpiSub;

    /**
     *
     * @var string
     */
    public $otherSub;

    /**
     *
     * @var string
     */
    public $phoneSub;

    /**
     *
     * @var string
     */
    public $deductWage;

    /**
     *
     * @var string
     */
    public $totalSalary;

    /**
     *
     * @var string
     */
    public $otherHold;

    /**
     *
     * @var string
     */
    public $socialHold;

    /**
     *
     * @var string
     */
    public $cpfHold;

    /**
     *
     * @var string
     */
    public $taxWage;

    /**
     *
     * @var string
     */
    public $taxHold;

    /**
     *
     * @var string
     */
    public $totalHold;

    /**
     *
     * @var string
     */
    public $finalPay;

    /**
     *
     * @var integer
     */
    public $createAt;

    /**
     *
     * @var integer
     */
    public $createBy;

    /**
     * Initialize method for model.
     */
    public function initialize()
    {
        parent::initialize();
    }

    /**
     * Returns table name mapped in the model.
     *
     * @return string
     */
    public function getSource()
    {
        return 'XHHrm_Payroll';
    }

    /**
     * Allows to query a set of records that match the specified conditions
     *
     * @param mixed $parameters
     * @return Payroll[]|Payroll|\Phalcon\Mvc\Model\ResultSetInterface
     */
    public static function find($parameters = null)
    {
        return parent::find($parameters);
    }

    /**
     * Allows to query the first record that match the specified conditions
     *
     * @param mixed $parameters
     * @return Payroll|\Phalcon\Mvc\Model\ResultInterface
     */
    public static function findFirst($parameters = null)
    {
        return parent::findFirst($parameters);
    }

    public static function getMap()
    {
        return [
            'basicWage'   => '基本工资',
            'phoneWage'   => '通讯费补贴',
            'totalSub'    => '加班、补贴、绩效',
            'overtimeSub' => '加班',
            'kpiSub'      => '绩效',
            'otherSub'    => '补贴',
            'phoneSub'    => '话补',
            'deductWage'  => '应扣',
            'totalPay'    => '应发工资',
            'otherHold'   => '其他',
            'socialHold'  => '社保',
            'cpfHold'     => '公积金',
            'taxWage'     => '计税额',
            'taxHold'     => '个税',
            'totalHold'   => '小计',
            'finalPay'    => '薪金总额',
        ];
    }

    /**
     * 填充数据
     *
     * @param $data
     * @return Payroll
     *
     * create by ck 20181221
     */
    public function fill($data)
    {
        $map = array_flip(self::getMap());
        foreach ($map as $target => $field) {
            $this->$field = Tool::getValue($data, $target) ?: 0;
        }
        return $this;
    }

    /**
     * 加密
     *
     * @param $key
     * @return bool
     *
     * create by ck 20181221
     */
    public function encrypt($key)
    {
        $crypt = Di::getDefault()->get('crypt');
        $finalKey = md5("{$key}|{$this->key}|{$this->month}|{$this->userId}");
        foreach (array_keys(self::getMap()) as $field) {
            $this->$field = $crypt->encryptBase64(strval($this->$field), $finalKey);
        }
        return true;
    }

    /**
     * 解密
     *
     * @param $key
     * @return bool
     *
     * create by ck 20181221
     */
    public function decrypt($key)
    {
        $crypt = Di::getDefault()->get('crypt');
        $finalKey = md5("{$key}|{$this->key}|{$this->month}|{$this->userId}");
        foreach (array_keys(self::getMap()) as $field) {
            $this->$field = $crypt->decryptBase64($this->$field, $finalKey);
        }
        return true;
    }
}
